package com.teame.readbaby;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.teame.readbaby.BaseActivity.DataCallback;

import com.teame.readbaby.parser.TopicListParser;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.Logger;
import com.teame.readbaby.vo.AddressListInfo;
import com.teame.readbaby.vo.TopicListInfo;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class TopicProductlistActivity extends BaseActivity {

	private ListView categoryList;
	//private List<Map<String, String>> data;
	private String Tag="TopicProductlistActivity";
	private List<TopicListInfo> list;
	
    static int[] images =
		{
    	R.drawable.a1,R.drawable.b2,
    	R.drawable.c3,R.drawable.d4,
    	R.drawable.e5,R.drawable.f6,
    	R.drawable.g7,R.drawable.h8
    	
		};

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void findViewById() {
		categoryList = (ListView) this.findViewById(R.id.categoryList);

	}

	@Override
	protected void loadViewLayout() {
		this.setContentView(R.layout.fastsale_activity);

	}

	@Override
	protected void processLogic() {
		String url = ConstantValue.HOST + ConstantValue.TOPIC;
//		 Map<String, String> params=new HashMap<String, String>();
//		 params.put("page", "1");
//		 params.put("page", "1");
		this.getDataFromServer(url, null, new TopicListParser(),
				new DataCallback<List<TopicListInfo>>() {

					@Override
					public void processData(List<TopicListInfo> paramObject,
							boolean paramBoolean) {

//						data = new ArrayList<Map<String, String>>();
//						for (TopicListInfo info : paramObject) {
////							Map<String, String> map = new HashMap<String, String>();
////							map.put("专题活动ID", info.getId() + "");
////							map.put("专题活动名称", info.getName());
////							map.put("图片URL", info.getPic());
////							data.add(map);
//							
//							Logger.i(TAG, info.toString());
//							
//						}
//						// String[] from = { "专题活动ID", "专题活动名称", "图片URL"};
						
						list=paramObject;
						categoryList.setAdapter(new MyAdapter());
					}

				});

	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = View.inflate(context,
						R.layout.fastsale_child_list, null);
				holder = new ViewHolder();
//				holder.lv_categoryList = (ListView) convertView
//						.findViewById(R.id.lv_categoryList);
				holder.tv_title=(TextView) convertView.findViewById(R.id.tv_fastsale_item);
				holder.iv_image=(ImageView) convertView.findViewById(R.id.iv_fastsale_item);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			
			TopicListInfo info = list.get(position);
			
			holder.tv_title.setText(info.getName()+"  "+new Date().toLocaleString());
			//图片获取
//			String url=info.getPic();
			holder.iv_image.setBackgroundResource(images[position]);
			return convertView;
		}

	}

	@Override
	protected void setListener() {
		categoryList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				TopicListInfo info = list.get(position);
				int idd = info.getId();
				Intent  intent = new Intent(TopicProductlistActivity.this, ProductListActivity.class);
				intent.putExtra("id", idd+"");
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onClickEvent(View paramView) {
		// TODO Auto-generated method stub

	}

	private static class ViewHolder {
		TextView tv_title;
		ImageView iv_image;
	}
}
